package com.tongji.seller.service
{
	import com.tongji.seller.events.AddStoreItemEvent;
	import com.tongji.seller.events.GetSavedProductEvent;
	import com.tongji.seller.events.GetStoreItemInfoEvent;
	import com.tongji.seller.events.GetStoreItemsEvent;
	import com.tongji.seller.events.UpdateItemImageEvent;
	import com.tongji.seller.model.StoreProductModel;
	import com.tongji.seller.model.UserModel;
	
	import flash.system.Security;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	
	import org.robotlegs.mvcs.Actor;
	
	import versionBase1Custom0.a3d.A3D;
	
	/**
	 * 
	 * @author jiarong
	 * 2011-12-6
	 *
	 */
	
	public class StoreProductService extends Actor
	{
		private var ws:WebService;
		[Inject]
		public var userModel:UserModel;
		
		[Inject]
		public var productModel:StoreProductModel;
		public function StoreProductService()
		{
			super();
			ws = new WebService();
			ws.wsdl = "http://share.rryjw.com/VRService.asmx?wsdl";
			ws.useProxy = false;
			ws.loadWSDL();
			Security.loadPolicyFile("http://img.rryjw.com/crossdomain.xml");
		}
		
		/**
		 * 获取店铺保存的宝贝
		 * @param userId
		 * @param page_no
		 * @param pageSize
		 * 
		 */		
		public function getStoreItem(userId:Number, page_no:int, pageSize:int):void
		{
			ws.GetStoreItemInfo(userId, page_no, pageSize);
			ws.GetStoreItemInfo.addEventListener(ResultEvent.RESULT, getStoreItemResult);
			ws.GetStoreItemInfo.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function getStoreProduct(userId:Number, page_no:int, pageSize:int):void
		{
			ws.GetStoreItemInfoTwo(userId, page_no, pageSize);
			ws.GetStoreItemInfoTwo.addEventListener(ResultEvent.RESULT, getStoreProductResult);
			ws.GetStoreItemInfoTwo.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		/**
		 * 获取店铺所有宝贝
		 * @param nick
		 * @param sQ
		 * @param page_no
		 * @param page_size
		 * 
		 */		
		public function getAllItem(nick:String, sQ:String, page_no:int, page_size:int):void
		{
			ws.GetStoreItems(nick, sQ, page_no, page_size);
			ws.GetStoreItems.addEventListener(ResultEvent.RESULT, getAllItemResult);
			ws.GetStoreItems.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 添加店铺宝贝场景图片 
		 * @param productId
		 * @param userId
		 * @param sImg
		 * 
		 */		
		public function addStoreItemImgInfo(productId:int, userId:int, sImg:Array):void
		{
			ws.AddStoreItemImgInfo(productId, userId, sImg);
			ws.AddStoreItemImgInfo.addEventListener(ResultEvent.RESULT, addStoreItemResult);
			ws.AddStoreItemImgInfo.addEventListener(FaultEvent.FAULT, faultHandler);
		}

		/**
		 * 更新店铺宝贝图片 
		 * @param productId
		 * @param sImg
		 * 
		 */		
		public function updateStoreItemImgInfo(productId:int, sImg:Array):void
		{
			ws.UpdateStoreItemImgInfo(productId, sImg);
			ws.UpdateStoreItemImgInfo.addEventListener(ResultEvent.RESULT, updateStoreItemResult);
			ws.UpdateStoreItemImgInfo.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		/**
		 * 添加用户选择的宝贝
		 * @param product
		 * 
		 */		
		public function addStoreItemInfo(product:String):void
		{
			ws.AddStoreItemInfoList(product);
			ws.AddStoreItemInfoList.addEventListener(ResultEvent.RESULT, addStoreItemInfoResult);
			ws.AddStoreItemInfoList.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 获取店铺保存的所有宝贝
		 * @param userID
		 * 
		 */			
		public function getAllStoreItemInfo(userID:Number):void
		{
			ws.GetStoreItemInfoAll(userID);
			ws.GetStoreItemInfoAll.addEventListener(ResultEvent.RESULT, getAllStoreItemResult);
			ws.GetStoreItemInfoAll.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 根据id，删除某一宝贝 
		 * @param proId
		 * 
		 */		
		public function deletePro(proId:int):void
		{
			ws.DeleteProduct(proId);
			ws.DeleteProduct.addEventListener(ResultEvent.RESULT, deleteProductResult);
			ws.DeleteProduct.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		private function getStoreItemResult(event:ResultEvent):void
		{
			dispatch( new GetStoreItemInfoEvent(GetStoreItemInfoEvent.UPDATE_STORE_ITEM, userModel.currentUserID, 1, 10, event.result as String));
		}
		
		private function getStoreProductResult(event:ResultEvent):void
		{
			dispatch( new GetStoreItemInfoEvent(GetStoreItemInfoEvent.UPDATE_STORE_PRODUCT, userModel.currentUserID, 1, 10, event.result as String));
		}
		private function getAllItemResult(event:ResultEvent):void
		{
			dispatch( new GetStoreItemsEvent(GetStoreItemsEvent.UPDATE_ALL_ITEMS, "", "", 0, 0, event.result as String));
		}
		
		private function addStoreItemResult(event:ResultEvent):void
		{
			dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, 1, 3));
		}		
		
		private function updateStoreItemResult(event:ResultEvent):void
		{
			if(event.result)
			{
				Alert.show("您的宝贝更新成功");
				dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, 1, 3));
			}
			else
			{
				Alert.show("您的宝贝更新出问题了");
			}
		}
		
		private function addStoreItemInfoResult(event:ResultEvent):void
		{
			dispatch(new GetSavedProductEvent(GetSavedProductEvent.GET_SAVED_PRODUCT, userModel.currentUserID));
			dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, 1, 3));
		}
		
		private function getAllStoreItemResult(event:ResultEvent):void
		{
			dispatch(new GetSavedProductEvent(GetSavedProductEvent.UPDATE_SAVED_PRODUCT, 0, event.result as String));
		}
		
		private function deleteProductResult(event:ResultEvent):void
		{
			if(event.result)
			{
				Alert.show("您的宝贝删除成功");
				productModel.removeProduct();
				dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, 1, 3));
			}
			else
			{
				
			}
		}
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show("失败" + event.fault.toString());
		}
	}
}